<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/12
 * Time: 13:44
 */
namespace WeChatDeveloper\app;

use Endroid\QrCode\QrCode;
use app\common\lib\Aes;

class MiniQrcode
{

    /**
     * @name 获取二维码(不是小程序的二维码)
     * @type get
     * @params token
     * @params params
     * @return json
     */
    public function MasterCode($url, $params, $data = [])
    {
        $qrcode = new QrCode();
        $aes = new Aes();
        $url = $url . '&' . $params . '=' . $aes->encrypt(json_encode($data));
        $qrcode->setText($url);
        header("Content-type:" . $qrcode->getContentType());
        return $qrcode->writeString();

    }

    /**
     * @name 获取小程序的二维码
     * @type get
     * @params  page 小程序需要跳到的页面
     * @params  appid
     * @params  secret
     * @params  type 生成的二维码类型
     * @params params
     * @params  data=[]   数组
     * @params width 生成二维码宽度
     * @params $auto_color = false
     * @params  $line_color = ["r" => "0", "g" => "0", "b" => "0"] 二维码颜色
     * @params  $outType = null 返回类型
     */
    public function ClientCode($appid, $secret, $page, $type,$params,$data = [], $width = 430, $auto_color = false, $line_color = ["r" => "0", "g" => "0", "b" => "0"], $outType = null)
    {

        $access_token = $this->AccessToken($appid, $secret);
        $aes = new Aes();
        //echo $aes->encrypt(json_encode($data));exit;
        $page = $page.'?'.$params.'='.$aes->encrypt(json_encode($data));

        $dataR = [
            'path' => $page,
            'width' => $width,
        ];
        if ($type != 3) {
            $dataR['auto_color'] = $auto_color;
            $dataR['line_color'] = $line_color;
        }
        $url = "";
        switch ($type) {
            case 1:
                $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' . $access_token;
                break;
            case 2:
                $dataR['scene'] = 1;
                $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
                break;
            case 3:
                $url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=' . $access_token;
                break;
        }
        $res = request_post($url, $this->arr2json($dataR));
        return $res;
    }

    /**
     * @name 获取access_token
     * @type post
     * @params appid
     * @params secret
     * @return json
     */
    public function AccessToken($appid,$secret){
        $url =  'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
        $res = request_post($url);
        $res = json_decode($res,true);
        return $res['access_token'];

    }

    /**
     * 数组转xml内容
     * @param array $data
     * @return null|string|string
     */
    public static function arr2json($data)
    {
        return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function ($matches) {
            return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");
        }, ($jsonData = json_encode($data)) == '[]' ? '{}' : $jsonData);
    }


}